<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 登录表单数据
const loginUsername = ref('')
const loginPassword = ref('')
const loginError = ref('')

// 注册表单数据
const registerUsername = ref('')
const registerPassword = ref('')
const registerConfirmPassword = ref('')
const registerError = ref('')
const registerSuccess = ref('')

// 当前显示的表单类型
const activeForm = ref('login')

// 切换表单
const switchToRegister = () => {
  activeForm.value = 'register'
  loginError.value = ''
  registerError.value = ''
  registerSuccess.value = ''
}

const switchToLogin = () => {
  activeForm.value = 'login'
  loginError.value = ''
  registerError.value = ''
  registerSuccess.value = ''
}

// 登录处理
const handleLogin = () => {
  // 简单的表单验证
  if (!loginUsername.value || !loginPassword.value) {
    loginError.value = '请输入用户名和密码'
    return
  }

  // 从localStorage中获取注册的用户信息
  const existingUsers = JSON.parse(localStorage.getItem('users') || '[]')
  
  // 查找匹配的用户
  const matchedUser = existingUsers.find(user => 
    user.username === loginUsername.value && user.password === loginPassword.value
  )
  
  // 模拟登录API调用
  setTimeout(() => {
    if (matchedUser) {
      // 登录成功
      localStorage.setItem('user', JSON.stringify({ username: loginUsername.value, isLoggedIn: true }))
      router.push('/home')
    } else {
      // 登录失败
      loginError.value = '用户名或密码错误'
    }
  }, 800)
}

// 注册处理
const handleRegister = () => {
  // 简单的表单验证
  if (!registerUsername.value || !registerPassword.value || !registerConfirmPassword.value) {
    registerError.value = '请填写所有必填字段'
    registerSuccess.value = ''
    return
  }

  if (registerPassword.value !== registerConfirmPassword.value) {
    registerError.value = '两次输入的密码不一致'
    registerSuccess.value = ''
    return
  }

  if (registerPassword.value.length < 6) {
    registerError.value = '密码长度不能少于6位'
    registerSuccess.value = ''
    return
  }

  // 检查用户是否已存在
  const existingUsers = JSON.parse(localStorage.getItem('users') || '[]')
  const userExists = existingUsers.some(user => user.username === registerUsername.value)
  
  if (userExists) {
    registerError.value = '用户名已存在'
    registerSuccess.value = ''
    return
  }

  // 模拟注册API调用
  setTimeout(() => {
    // 保存用户信息到localStorage
    existingUsers.push({
      username: registerUsername.value,
      password: registerPassword.value
    })
    localStorage.setItem('users', JSON.stringify(existingUsers))
    
    // 注册成功
    registerSuccess.value = '注册成功！请登录'
    registerError.value = ''
    
    // 清空表单
    registerUsername.value = ''
    registerPassword.value = ''
    registerConfirmPassword.value = ''
    
    // 自动切换到登录表单
    setTimeout(() => {
      switchToLogin()
    }, 1500)
  }, 800)
}
</script>

<template>
  <div class="auth-container">
    <div class="auth-wrapper">
      <!-- 表单头部 -->
      <div class="auth-header">
        <h1>欢迎使用</h1>
        <p>请登录或注册账号</p>
      </div>
      
      <!-- 表单切换选项卡 -->
      <div class="auth-tabs">
        <button 
          class="tab-button" 
          :class="{ active: activeForm === 'login' }"
          @click="switchToLogin"
        >
          登录
        </button>
        <button 
          class="tab-button" 
          :class="{ active: activeForm === 'register' }"
          @click="switchToRegister"
        >
          注册
        </button>
      </div>
      
      <!-- 登录表单 -->
      <div v-if="activeForm === 'login'" class="auth-form">
        <div class="form-group">
          <label for="loginUsername">用户名</label>
          <input 
            id="loginUsername"
            v-model="loginUsername" 
            type="text" 
            placeholder="请输入用户名"
            @keyup.enter="handleLogin"
          >
        </div>
        
        <div class="form-group">
          <label for="loginPassword">密码</label>
          <input 
            id="loginPassword"
            v-model="loginPassword" 
            type="password" 
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
          >
        </div>
        
        <button class="auth-btn" @click="handleLogin">登录</button>
        
        <div v-if="loginError" class="error-message">
          {{ loginError }}
        </div>
      </div>
      
      <!-- 注册表单 -->
      <div v-else class="auth-form">
        <div class="form-group">
          <label for="registerUsername">用户名</label>
          <input 
            id="registerUsername"
            v-model="registerUsername" 
            type="text" 
            placeholder="请设置用户名"
            @keyup.enter="handleRegister"
          >
        </div>
        
        <div class="form-group">
          <label for="registerPassword">密码</label>
          <input 
            id="registerPassword"
            v-model="registerPassword" 
            type="password" 
            placeholder="请设置密码（至少6位）"
            @keyup.enter="handleRegister"
          >
        </div>
        
        <div class="form-group">
          <label for="registerConfirmPassword">确认密码</label>
          <input 
            id="registerConfirmPassword"
            v-model="registerConfirmPassword" 
            type="password" 
            placeholder="请再次输入密码"
            @keyup.enter="handleRegister"
          >
        </div>
        
        <button class="auth-btn" @click="handleRegister">注册</button>
        
        <div v-if="registerError" class="error-message">
          {{ registerError }}
        </div>
        
        <div v-if="registerSuccess" class="success-message">
          {{ registerSuccess }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url('https://picsum.photos/id/1015/1920/1080');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 20px;
}

.auth-wrapper {
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 600px;
}

.auth-header {
  text-align: center;
  margin-bottom: 30px;
}

.auth-header h1 {
  font-size: 28px;
  color: #333;
  margin-bottom: 8px;
}

.auth-header p {
  color: #666;
  font-size: 16px;
}

.auth-tabs {
  display: flex;
  margin-bottom: 30px;
  border-bottom: 2px solid #f0f0f0;
}

.tab-button {
  flex: 1;
  padding: 12px;
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  color: #666;
  transition: all 0.3s;
  border-bottom: 3px solid transparent;
}

.tab-button:hover {
  color: #42b983;
}

.tab-button.active {
  color: #42b983;
  border-bottom-color: #42b983;
  font-weight: 600;
}

.auth-form {
  animation: fadeIn 0.3s ease-in-out;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 16px;
  transition: border-color 0.3s, box-shadow 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #42b983;
  box-shadow: 0 0 0 3px rgba(66, 185, 131, 0.1);
}

.auth-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #42b983 0%, #3aa876 100%);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 10px;
}

.auth-btn:hover {
  background: linear-gradient(135deg, #3aa876 0%, #369d6d 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(66, 185, 131, 0.3);
}

.error-message {
  color: #f44336;
  font-size: 14px;
  text-align: center;
  margin-top: 16px;
  padding: 10px;
  background-color: #ffebee;
  border-radius: 4px;
}

.success-message {
  color: #42b983;
  font-size: 14px;
  text-align: center;
  margin-top: 16px;
  padding: 10px;
  background-color: #e8f5e8;
  border-radius: 4px;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .auth-wrapper {
    padding: 30px 20px;
  }
  
  .auth-header h1 {
    font-size: 24px;
  }
  
  .form-group input {
    padding: 10px 12px;
  }
  
  .auth-btn {
    padding: 12px;
  }
}
</style>